package com.sandking.db.jedis;

import java.util.ArrayList;
import com.sandking.db.bean.Zb;
import com.sandking.tools.SK_Plus;
import com.sandking.tools.SK_Jedis;
import java.util.List;
import java.util.Set;
import com.sandking.db.dao.ZbDao;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Jedis;

public class ZbJedis{
	/**
	 * 根据( id ) 查询
	 */
	public static Zb getById(int id){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		Zb zb = null;
		try{
			String key = "Zb_Object";
			String field = SK_Plus.b("id:",id).e();
			zb = Zb.createForJson(jedis.hget(key,field));
			if(zb == null){
				zb = ZbDao.getById(id);
				if(zb!=null){
					loadCache(zb);
				}
			}
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		
		return zb;
	}
	
	/**
	 * 根据( tempid  攻击人id ) 查询
	 */
	public static Zb getByTempidGjrid(int tempid, int gjrid){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		Zb zb = null;
		try{
			String key = "Zb_Index";
			String field = SK_Plus.b("tempid, gjrid:",tempid, gjrid).e();
			String primaryKey = jedis.hget(key,field);
			if(primaryKey!=null){
				zb = getById(Integer.valueOf(primaryKey));
			}
			if(zb == null){
				zb = ZbDao.getByTempidGjrid(tempid, gjrid);
				if(zb!=null){
					loadCache(zb);
				}
			}
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		
		return zb;
	}
	
	/**
	 * 根据( 攻击人id ) 查询
	 */
	public static List<Zb> getByGjrid(int gjrid){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		List<Zb> zbs = new ArrayList<Zb>();
		try{
			String key = "Zb_Index";
			String field = SK_Plus.b(key,"gjrid:",gjrid).e();
			Set<String> setStr = jedis.smembers(field);
			if(setStr!=null){
				key = "Zb_Object";
				String[] fieldArray = (String[]) setStr.toArray();
				List<String> jsons = jedis.hmget(key,fieldArray);
				zbs = Zb.createForJson(jsons);
			}else{
				zbs = ZbDao.getByGjrid(gjrid);
				if(zbs!=null && !zbs.isEmpty()){
					loadCaches(zbs);
				}
			}
			
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		return zbs;
	}
	
	/**
	 * 根据( 目标人id ) 查询
	 */
	public static List<Zb> getByMbrid(int mbrid){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		List<Zb> zbs = new ArrayList<Zb>();
		try{
			String key = "Zb_Index";
			String field = SK_Plus.b(key,"mbrid:",mbrid).e();
			Set<String> setStr = jedis.smembers(field);
			if(setStr!=null){
				key = "Zb_Object";
				String[] fieldArray = (String[]) setStr.toArray();
				List<String> jsons = jedis.hmget(key,fieldArray);
				zbs = Zb.createForJson(jsons);
			}else{
				zbs = ZbDao.getByMbrid(mbrid);
				if(zbs!=null && !zbs.isEmpty()){
					loadCaches(zbs);
				}
			}
			
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		return zbs;
	}
	

	public static void loadCache(Zb zb){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			loadCache(zb,p);
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	/**
	 * 加载一个缓存
	 */
	public static void loadCache(Zb zb,Pipeline p){
		// ---------------------- 主键索引 ----------------------
		String key = "Zb_Object";
		String field = SK_Plus.b("id:",zb.getId()).e();
		String data = zb.toJson();
		p.hset(key,field,data);
		
		// ---------------------- 唯一索引 ----------------------
		String primaryKey = String.valueOf(zb.getId());
		key = "Zb_Index";
		
		field = SK_Plus.b("tempid, gjrid:",zb.getTempid(),zb.getGjrid()).e();
		p.hset(key,field,primaryKey);
		
		// ---------------------- 聚集索引 ----------------------
		field = SK_Plus.b(key,"gjrid:",zb.getGjrid()).e();
		p.sadd(field, primaryKey);
		
		field = SK_Plus.b(key,"mbrid:",zb.getMbrid()).e();
		p.sadd(field, primaryKey);
		
	}
	
	/**
	 * 加载一组缓存
	 */
	public static void loadCaches(List<Zb> zbs){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			for(Zb zb : zbs){
				loadCache(zb,p);
			}
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	public static void clearCache(Zb zb){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			clearCache(zb,p);
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	/**
	 * 清空一个缓存
	 */
	public static void clearCache(Zb zb,Pipeline p){
		// ---------------------- 主键索引 ----------------------
		String key = "Zb_Object";
		String field = SK_Plus.b("id:",zb.getId()).e();
		p.hdel(key,field);
		
		// ---------------------- 唯一索引 ----------------------
		key = "Zb_Index";
		
		field = SK_Plus.b("tempid, gjrid:",zb.getTempid(),zb.getGjrid()).e();
		p.hdel(key,field);
		
		// ---------------------- 聚集索引 ----------------------
		String primaryKey = String.valueOf(zb.getId());
		field = SK_Plus.b(key,"gjrid:",zb.getGjrid()).e();
		p.srem(field, primaryKey);
		
		field = SK_Plus.b(key,"mbrid:",zb.getMbrid()).e();
		p.srem(field, primaryKey);
		
	}
	/**
	 * 清空一组缓存
	 */
	public static void clearCaches(List<Zb> zbs){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			for(Zb zb : zbs){
				clearCache(zb,p);
			}
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	public static Zb insert(Zb zb){
		zb = ZbDao.insert(zb);
		if(zb!=null){
			loadCache(zb);
		}
    	return zb;
    }
    
    public static Zb update(Zb zb){
    	zb = ZbDao.update(zb);
    	if(zb!=null){
			loadCache(zb);
		}
    	return zb;
    }
    
    public static boolean delete(Zb zb){
    	boolean bool = ZbDao.delete(zb);
    	if(bool){
    		clearCache(zb);
    	}
    	return bool;
    }
}